<?php
	include("../Includes/FusionCharts.php");
	include("../Includes/FusionCharts_Gen.php");
	include("../Includes/DBConn.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
	<HEAD>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>UPLB DV</title>
		
		<script language="Javascript" src="../js/FusionCharts.js"></script>
		<script type="text/javascript" src="../fancybox/jquery.min.js"></script>
		<script type="text/javascript" src="../fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
		<script type="text/javascript" src="../fancybox/jquery.fancybox-1.3.4.pack.js"></script>
		<script type="text/javascript">

		$(document).ready(function() {
			$("a#example1").fancybox();

			$("a#example2").fancybox({
				'overlayShow'	: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});

			$("a#example3").fancybox({
				'transitionIn'	: 'none',
				'transitionOut'	: 'none'	
			});

			$("a#example4").fancybox({
				'opacity'		: true,
				'overlayShow'	: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'none'
			});

			$("a#example5").fancybox();

			$("a#example6").fancybox({
				'titlePosition'		: 'outside',
				'overlayColor'		: '#000',
				'overlayOpacity'	: 0.9
			});

			$("a#example7").fancybox({
				'titlePosition'	: 'inside'
			});

			$("a#example8").fancybox({
				'titlePosition'	: 'over'
			});

			$("a[rel=example_group]").fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});

			$("#various1").fancybox({
					'titlePosition'		: 'inside',
					'transitionIn'		: 'none',
					'transitionOut'		: 'none'
			});

			$("#various2").fancybox();

			$("#various3").fancybox({
					'width'				: '75%',
					'height'			: '75%',
					'autoScale'			: false,
					'transitionIn'		: 'elastic',
					'transitionOut'		: 'elastic',
					'type'				: 'iframe'
			});

			$("#various4").fancybox({
					'padding'			: 0,
					'autoScale'			: false,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none'
			});

		});

	</script>

		<link rel="stylesheet" href="../fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />

	</HEAD>

	<BODY bgcolor="white">
	<CENTER>
			<?php
				$subject = $_GET['subject'];
				//echo $subject;
				if($subject=="default"){
					$snfirst = 2007;
					$snlast = 36018;
					$semester = 1;
					$kindofgraph = 'FCF_LINE';
				}
				else{
					$snfirst = $_POST['sn_first'];
					$snlast = $_POST['sn_last'];
					$semester = 1;
					$kindofgraph = 'FCF_LINE';
					
				}
				
					$snnumber = $snfirst.$snlast;
					$studentnumber = $snfirst."-".$snlast;
					
					// Connect to the DB
				$link = connectToDB();

				//$strXML will be used to store the entire XML document generated
				//Generate the graph element
				//fetch the name of the specified student number
				$strQuery0 = "select * from tempStudents where studentNumber='$snnumber'";
				$result0 = mysql_query($strQuery0) or die(mysql_error());
				$ors = mysql_fetch_array($result0);
				$fullname = $ors['firstName']." ".$ors['lastName'];
				
				
				
				
				$strQuery1 = "select mayts from tempStudents where studentNumber='$snnumber'";
				$result1 = mysql_query($strQuery1) or die(mysql_error());
				$ors1 = mysql_fetch_array($result1);
				$maxallowed = $ors1['mayts'];
				$ay = $snfirst;
				$tempsem = $semester;
				
					$FC = new FusionCharts("MSColumn3DLineDY","850","440");
					$FC->setSWFPath("../FusionCharts/");
					$strParam="caption=General Weighted Average (Current and Running); subcaption=$fullname; xAxisName=Semester; pYAxisName=Current GWA; sYAxisName=Running GWA; decimalPrecision=2; yaxisminvalue=0; yaxismaxvalue=5; numdivlines=9";
					$FC->setChartParams($strParam);

					while($ay <= $snfirst + $maxallowed){
					// Fetch all factory records
						
							$strQuerytotal = "select SUM(units) as totalunits from tempSubjects where studentNumber='$snnumber' and academicYear='$ay' and semester='$tempsem'";
							$resulttotal = mysql_query($strQuerytotal) or die(mysql_error());
							$orstotal = mysql_fetch_array($resulttotal);
							
							if($tempsem == 1){
								$semname ="1st";
							}
							else if($tempsem == 2){
								$semname = "2nd";
							}
							else if($tempsem == 3){
								$semname = "3rd";
				}
							
							$ayt = $semname." ".$ay."".($ay+1);
							
							if($orstotal['totalunits']>0){
								$FC->addCategory($ayt);
							}
							
							if($tempsem == 3){
								$ay=$ay + 1;
								$tempsem=1;
							}
							else{
								$tempsem++;
							}
					}
					
					$ay = $snfirst;
					$tempsem = $semester;
					$runninggwa = 0;
					$runningtotal = 0;
					$FC->addDataset("Running GWA", "parentYaxis=S"); 
					while($ay <= $snfirst + $maxallowed){
					// Fetch all factory records
						
							$strQuerytotal = "select SUM(units) as totalunits from tempSubjects where studentNumber='$snnumber' and academicYear='$ay' and semester='$tempsem'";
							$resulttotal = mysql_query($strQuerytotal) or die(mysql_error());
							$orstotal = mysql_fetch_array($resulttotal);
							
							
							if($orstotal['totalunits']>0){
								$strQuery = "select * from tempSubjects where studentNumber='$snnumber' and academicYear='$ay' and semester='$tempsem'";
								$result = mysql_query($strQuery) or die(mysql_error());
							
								$total = 0;
								$totalu = $orstotal['totalunits'];
							
								//Iterate through each factory
								if ($result) {
									while($ors = mysql_fetch_array($result)) {
										if($ors['grade']>5){
											$totalu = $totalu - $ors['units'];
										}
										else{
											$total = $total + ($ors['grade'] * $ors['units']);
										}
									}
								}
								$gwa = $total/$totalu;
								if($runninggwa == 0){
									$runninggwa = $gwa;
									$runningtotal = $totalu;
								}
								else{
									$runninggwa = (($runninggwa * $runningtotal) + ($gwa * $totalu)) / ($runningtotal + $totalu) ;
									$runningtotal = $runningtotal + $totalu;
								}
								$FC->addChartData($runninggwa);
								//$strXML .= "<set name='" . $semester." Semester ".$ay. "' value='" . $gwa . "' />";
							}//end of outside if
							if($tempsem == 3){
								$ay=$ay + 1;
								$tempsem=1;
							}
							else{
								$tempsem++;
							}
						
					}//end of while
					
					$ay = $snfirst;
					$FC->addDataset("Current GWA", "showValues=0"); 
					while($ay <= $snfirst + $maxallowed){
					// Fetch all factory records
						
							$strQuerytotal = "select SUM(units) as totalunits from tempSubjects where studentNumber='$snnumber' and academicYear='$ay' and semester='$semester'";
							$resulttotal = mysql_query($strQuerytotal) or die(mysql_error());
							$orstotal = mysql_fetch_array($resulttotal);
							
							
							if($orstotal['totalunits']>0){
								$strQuery = "select * from tempSubjects where studentNumber='$snnumber' and academicYear='$ay' and semester='$semester'";
								$result = mysql_query($strQuery) or die(mysql_error());
							
								$total = 0;
								$totalu = $orstotal['totalunits'];
							
								//Iterate through each factory
								if ($result) {
									while($ors = mysql_fetch_array($result)) {
										if($ors['grade']>5){
											$totalu = $totalu - $ors['units'];
										}
										else{
											$total = $total + ($ors['grade'] * $ors['units']);
										}
									}
								}
								$gwa = $total/$totalu;
								//echo $gwa;
								$FC->addChartData($gwa);
								//$strXML .= "<set name='" . $semester." Semester ".$ay. "' value='" . $gwa . "' />";
							}//end of outside if
							if($semester == 3){
								$ay=$ay + 1;
								$semester=1;
							}
							else{
								$semester++;
							}
						
					}//end of while
					
					//close the link
					mysql_close($link);
					$FC->renderChart();	
					
					//Finally, close <graph> element
					//$strXML .= "</graph>";
					//$graph = $kindofgraph.".swf";
					//Create the chart - Pie 3D Chart with data from $strXML
					//echo renderChart("../FusionCharts/$graph", "", $strXML, "FactorySum", 850, 430);
				
			?>
	</CENTER>
	</BODY>
</HTML>